<?php
//check1
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\AeducationModel;

class AeducationController extends Controller {
    public function index(){
    	if(session('?user')){
    		$this->assign('user',I('session.user'));
    		$reserve = M('Acontact');
    		$data1['email'] = I('session.user');
    		$data1['submit'] = '0';
    		$data2['email'] = I('session.user');
    		$data2['submit'] = '1';
    		$result1=$reserve->where($data1)->select();
    		$result2=$reserve->where($data2)->select();
    		if($result1){
    			$table = M('Aeducation');
    			$result=$table->where($data1)->select();
    			if($result)
    				$this->assign('data', $result[0]);
    			$this->display();
    		}
    		else if($result2)
    			redirect(U('Myhomepage/index'));  		
    		else{
    			redirect(U('Student/index'));
    		}
    	} 	
    	else
    		redirect(U('Login/index'));
    }
    
    public function back(){
    	if(session('?user')){
    		$this->assign('user',I('session.user'));
    		redirect(U('Acontact/index'));
    	}
    	else
    		redirect(U('Login/index'));
    }
    
    public function save(){
   		if(IS_POST&&session('?user')){
	    	$table = D('Aeducation');
	    	$data['email']=session('user');	    	
	    	$data['highschool'] = I('post.highschool');
	    	$data['ey_highschool'] = I('post.ey_highschool');
	    	$data['edu1'] = I('post.edu1');
	    	$data['degree_edu1'] = I('post.degree_edu1');
	    	$data['major_edu1'] = I('post.major_edu1');
	    	$data['by_edu1'] = I('post.by_edu1');
	    	$data['ey_edu1'] = I('post.ey_edu1');
	    	$data['gpa_edu1'] = I('post.gpa_edu1');	  
	    	$data['gpa_total_edu1'] = I('post.gpa_total_edu1');
	    	$data['edu2'] = I('post.edu2');
	    	$data['degree_edu2'] = I('post.degree_edu2');
	    	$data['major_edu2'] = I('post.major_edu2');
	    	$data['by_edu2'] = I('post.by_edu2');
	    	$data['ey_edu2'] = I('post.ey_edu2');
	    	$data['gpa_edu2'] = I('post.gpa_edu2');	
	    	$data['gpa_total_edu2'] = I('post.gpa_total_edu2');
	    	$data['edu3'] = I('post.edu3');
	    	$data['degree_edu3'] = I('post.degree_edu3');
	    	$data['major_edu3'] = I('post.major_edu3');
	    	$data['by_edu3'] = I('post.by_edu3');
	    	$data['ey_edu3'] = I('post.ey_edu3');
	    	$data['gpa_edu3'] = I('post.gpa_edu3');
	    	$data['gpa_total_edu3'] = I('post.gpa_total_edu3');
	    	if ($data1=$table->create($data)){
	    		$user=I('session.user');
	    		$change=$table->where($data1)->select();
	    		$result=$table->where("email='$user'")->select();
	    		if($change){
	    			redirect(U('Aemployment/index'));
	    		}
	    		else{
		    		if($result){
		    			if($table->save($data1))
		    				redirect(U('Aemployment/index'));
		    			else{
		    				$error=$table->getError();
		    				$this->assign('message','保存失败');
		    				$this->assign('title1','保存失败');
		    				$this->assign('pagename','申请页面');
		    				$this->assign('data',$error);
		    				$this->error('保存失败！','index',3);
		    			}
		    		}
		    		else{
		    			if($table->add($data1))
		    				redirect(U('Aemployment/index'));
		    			else{
		    				$error=$table->getError();
		    				$this->assign('message','保存失败');
		    				$this->assign('title1','保存失败');
		    				$this->assign('pagename','申请页面');
		    				$this->assign('data',$error);
		    				$this->error('保存失败！','index',3);
		    			}
		    		}
	    		}		    		
	    	}
	    	else{
	    		$error=$table->getError();
	    		$this->assign('message','保存失败');
	    		$this->assign('title1','保存失败');
	    		$this->assign('pagename','申请页面');
	    		$this->assign('data',$error);
	    		$this->error('保存失败！','index',3);
	    	} 
    	}
    	else
    		$this->display('index');  		    	
    }
}